<template>
  <div class="login-page">
    <el-form :model="user" :rules="rule" label-width="80" ref="userForm">
      <div class="form-label">
        <h1>重置密码</h1>
      </div>
      <el-form-item label="账号" prop="username">
        <el-input type="text" v-model="user.username"></el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="code">
        <el-input style="width: 110px; margin-right: 5px;" type="text" v-model="user.code"/>
        <el-button :loading="sendLoading" type="primary" @click="sendCheckCode">验证码</el-button>
      </el-form-item>
      <el-form-item label="新密码" prop="password">
        <el-input type="password" v-model="user.password" show-password></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPassword">
        <el-input type="password" v-model="user.checkPassword" show-password></el-input>
      </el-form-item>
      <div class="register-button">
        <el-button :loading="loading" type="primary" style="width: 100%" @click="toResetPassword">重置密码</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { ref } from 'vue'
import { sendCheckCode, userResetPassword } from '@/api/user'
import { ElMessage } from 'element-plus'
import 'element-plus/es/components/message/style/css'

export default {
  name: 'user-change-password',
  setup () {
    const user = ref({
      username: '',
      code: '',
      codeOwner: '',
      password: '',
      checkPassword: ''
    })
    const checkPassword = function (rule, value, callback) {
      if (value) {
        if (user.value.password !== user.value.checkPassword) {
          callback(new Error('密码不一致'))
        }
      }
      callback()
    }
    return {
      user,
      sendLoading: ref(false),
      loading: ref(false),
      rule: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        nickname: [],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ],
        checkPassword: [
          { required: true, message: '请输入确认密码', trigger: 'blur' },
          { required: true, validator: checkPassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    sendCheckCode () {
      this.sendLoading = true
      if (this.user.username) {
        sendCheckCode(this.user).then(value => {
          this.user.codeOwner = value.data
          this.sendLoading = false
        })
      } else {
        this.sendLoading = false
        ElMessage({
          showClose: true,
          grouping: true,
          message: '请填写需要重置密码的账号',
          type: 'error'
        })
      }
    },
    toResetPassword () {
      this.loading = true
      this.$refs.userForm.validate(val => {
        if (val) {
          userResetPassword(this.user).then(value => {
            this.loading = false
            ElMessage({
              showClose: true,
              grouping: true,
              message: '密码已重置，请登录',
              type: 'success'
            })
            this.$refs.registerForm.resetFields()
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.register-button{
  margin-left: 50px;
  margin-right: 20px;
}
.login-page{
  width: 600px;
  margin: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 600px;
  background-color: #e3eeef;
}
.form-label{
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 30px 30px;
  padding-top: 0;
}
.login-addition{
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: space-between;
}
.el-form{
  width: 300px;
}
</style>
